/*
 * Copyright (C) 2011 JiangHongTiao <jjurco.sk_gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove301.jianghongtiao.liaad.prisoner.ga.selection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import sk.lieskove301.jianghongtiao.liaad.prisoner.agent.Agent;
import sk.lieskove301.jianghongtiao.liaad.prisoner.agent.AgentDescComparator;
import sk.lieskove301.jianghongtiao.liaad.prisoner.agent.AgentIncComparator;

/**
 * Date of create: May 26, 2011
 *
 * @author JiangHongTiao <jjurco.sk_gmail.com>
 * @version 2011.0526
 */
public class BestNSelection implements Selection{
    private double approxSelectionSize;

    public List<Agent> selectAgents(List<Agent> population) {
        Collections.sort(population, new AgentIncComparator());
        int select = (int) (approxSelectionSize*population.size());
        List<Agent> result = new ArrayList<Agent>();
        for (int i = 0; i < select; i++) {
            result.add(population.get(i));
        }
        return result;
    }

    public void setAproxSelectionSize(double percent) {
        if((percent <= 0) || (percent >= 1)){
            throw new IllegalArgumentException("percent should be in range 0..1. 0 and 1 are excluded.");
        }
        this.approxSelectionSize = percent;
    }
    
}
